{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "github地址:[https://github.com/cheesezh/python_design_patterns](https://github.com/cheesezh/python_design_patterns)\n",
    "\n",
    "## 适配器模式\n",
    "\n",
    "适配器模式，将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作[DP]。\n",
    "\n",
    "当系统的数据和行为都正确，但是接口不符时，我们应该考虑使用适配器模式，目的就是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类，但是接口又与复用环境要求不一致的情况。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特殊请求\n"
     ]
    }
   ],
   "source": [
    "class Target():\n",
    "    \"\"\"\n",
    "    Target类，这是客户所期待的接口。可以是具体或抽象的类，也可以是接口。\n",
    "    \"\"\"\n",
    "    def request(self):\n",
    "        print(\"普通请求\")\n",
    "        \n",
    "        \n",
    "class Adaptee():\n",
    "    \"\"\"\n",
    "    需要适配的类\n",
    "    \"\"\"\n",
    "    def specific_request(self):\n",
    "        print(\"特殊请求\")\n",
    "        \n",
    "        \n",
    "class Adapter(Target):\n",
    "    \"\"\"\n",
    "    适配器，通过内部包装一个Adaptee对象，把源接口转换成目标接口\n",
    "    \"\"\"\n",
    "    def __init__(self):\n",
    "        self.adaptee = Adaptee()\n",
    "        \n",
    "    def request(self):\n",
    "        self.adaptee.specific_request()\n",
    "        \n",
    "        \n",
    "def main():\n",
    "    target = Adapter()\n",
    "    target.request()\n",
    "    \n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 何时使用适配器模式？\n",
    "\n",
    "想使用一个已经存在的类，但如果它的接口，也就是它的方法和你的要求不想同时，就应该考虑使用适配器模式。\n",
    "\n",
    "对于公司内部独立开发的系统，类和方法名的规范应当在设计之初就规定好，当接口不相同时，首先不应该考虑使用适配器，而是应该考虑通过重构统一接口。\n",
    "\n",
    "只有在双方都不太容易修改的时候再使用适配器模式。\n",
    "\n",
    "但是如果设计之初，我们准备使用第三方开发组件，而这个组件的接口于我们自己的系统接口是不相同的，而我们也完全没有必要为了迎合它而改动自己的接口，此时尽管在开发的设计阶段，也就是可以考虑用适配器模式来解决接口不同的问题。\n",
    "\n",
    "## 题目\n",
    "\n",
    "用程序模拟姚明到国外打NBA初期依赖翻译的场景。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Forward FFF attack\n",
      "Guards GGG defense\n",
      "Center CCC jingong\n",
      "Center CCC fangshou\n"
     ]
    }
   ],
   "source": [
    "from abc import ABCMeta, abstractmethod\n",
    "\n",
    "\n",
    "class Player():\n",
    "    \n",
    "    __metaclass__ = ABCMeta\n",
    "    \n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        \n",
    "    @abstractmethod\n",
    "    def attack(self):\n",
    "        pass\n",
    "    \n",
    "    @abstractmethod\n",
    "    def defense(self):\n",
    "        pass\n",
    "    \n",
    "    \n",
    "class Forwards(Player):\n",
    "    \n",
    "    def attack(self):\n",
    "        print(\"Forward {} attack\".format(self.name))\n",
    "        \n",
    "    def defense(self):\n",
    "        print(\"Forward {} defense\".format(self.name))\n",
    "        \n",
    "        \n",
    "class Guards(Player):\n",
    "    \n",
    "    def attack(self):\n",
    "        print(\"Guards {} attack\".format(self.name))\n",
    "        \n",
    "    def defense(self):\n",
    "        print(\"Guards {} defense\".format(self.name))\n",
    "\n",
    "        \n",
    "class ForeignCenter():\n",
    "    \n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        \n",
    "    def jingong(self):\n",
    "        print(\"Center {} jingong\".format(self.name))\n",
    "        \n",
    "    def fangshou(self):\n",
    "        print(\"Center {} fangshou\".format(self.name))\n",
    "\n",
    "\n",
    "class Translator(Player):\n",
    "    \n",
    "    def __init__(self, name):\n",
    "        self.foreign_center = ForeignCenter(name)\n",
    "        \n",
    "    def attack(self):\n",
    "        self.foreign_center.jingong()\n",
    "        \n",
    "    def defense(self):\n",
    "        self.foreign_center.fangshou()\n",
    "        \n",
    "        \n",
    "forwards = Forwards(\"FFF\")\n",
    "forwards.attack()\n",
    "\n",
    "guards = Guards(\"GGG\")\n",
    "guards.defense()\n",
    "\n",
    "center = Translator(\"CCC\")\n",
    "center.attack()\n",
    "center.defense()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
